package com.basiclib.widget

import android.content.Context
import android.graphics.Color
import androidx.core.content.ContextCompat
import androidx.appcompat.widget.AppCompatTextView
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import com.basiclib.R
import com.jakewharton.rxbinding2.view.clicks
import org.jetbrains.anko.*

class CustomToolbar constructor(val mContext: Context, val attrs: AttributeSet?, val defStyleAttr: Int) :
    RelativeLayout(mContext, attrs, defStyleAttr) {
    private val TEXT_STYLE_NORMAL = 0
    private val TEXT_STYLE_BOLD = 2

    private lateinit var mToolbarContainer: RelativeLayout
    private lateinit var mTitleView: TextView
    private var mBackView: View? = null

    private var mToolbarTitle: String? = null
    private var mToolbarTitleBlod: Int? = null
    private var mToolbarTitleColor: Int = 0
    private var mToolbarTitleTextSize: Int = 16
    private var mToolbarPaddingLeft: Float = 0f
    private var mToolbarPaddingRight: Float = 0f
    private var mToolbarTitleGravity: Int = Gravity.CENTER
    private var mToolbarShowBack: Boolean = true
    private var mHeight: Int? = null
    private var mToolbarBackgroundColor: Int = 0


    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context) : this(context, null)

    init {
        val systemAttrs = intArrayOf(android.R.attr.actionBarSize)
        val a = context.obtainStyledAttributes(attrs, systemAttrs)
        mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT)
        a.recycle()
        obtainAttributes(mContext, attrs)
        setupContainer()
        initView()
    }

    private fun setupContainer() {
        mToolbarContainer = RelativeLayout(mContext)
        val containerParams =
            RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mHeight!!)
        mToolbarContainer.leftPadding = mToolbarPaddingLeft.toInt()
        mToolbarContainer.rightPadding = mToolbarPaddingRight.toInt()
        addView(mToolbarContainer, containerParams)
    }

    private fun obtainAttributes(context: Context, attrs: AttributeSet?) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.CustomToolbar)
        mToolbarTitle = ta.getString(R.styleable.CustomToolbar_title)
        mToolbarTitleColor =
                ta.getColor(R.styleable.CustomToolbar_title_color, ContextCompat.getColor(mContext, android.R.color.white))
        mToolbarTitleTextSize =
                ta.getInteger(R.styleable.CustomToolbar_title_size, 16)
        mToolbarPaddingLeft =
                ta.getDimension(R.styleable.CustomToolbar_padding_left, mContext.dip(20f).toFloat())
        mToolbarPaddingRight =
                ta.getDimension(R.styleable.CustomToolbar_padding_right, mContext.dip(20f).toFloat())
        mToolbarTitleBlod = ta.getInt(R.styleable.CustomToolbar_title_bold, TEXT_STYLE_NORMAL)
        mToolbarTitleGravity = ta.getInt(R.styleable.CustomToolbar_title_gravity, Gravity.CENTER)
        mToolbarShowBack = ta.getBoolean(R.styleable.CustomToolbar_show_back, true)
        mToolbarBackgroundColor = ta.getColor(R.styleable.CustomToolbar_back_ground_color, Color.parseColor("#23AFDC"))
        ta.recycle()
    }

    private fun initView() {
        mToolbarContainer.backgroundColor = mToolbarBackgroundColor
        mTitleView = AppCompatTextView(mContext)
        mTitleView.apply {
            text = mToolbarTitle
            textSize = mToolbarTitleTextSize.toFloat()
            paint.isFakeBoldText = mToolbarTitleBlod == TEXT_STYLE_BOLD
            textColor = mToolbarTitleColor
            singleLine = true
        }
        val titleParams = RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        when (mToolbarTitleGravity) {
            Gravity.RIGHT -> {
                titleParams.alignParentRight()
                titleParams.centerVertically()
            }
            Gravity.LEFT -> {
                mToolbarShowBack = false
                titleParams.alignParentLeft()
                titleParams.centerVertically()
            }
            Gravity.CENTER -> {
                titleParams.centerInParent()
            }
        }
        mToolbarContainer.addView(mTitleView, titleParams)

        if (mToolbarShowBack) {
            mBackView = View.inflate(mContext, R.layout.layout_back, null)
            val backViewParams =
                RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
            backViewParams.alignParentLeft()
            backViewParams.centerVertically()
            mToolbarContainer.addView(mBackView, backViewParams)
        }

    }

    fun setTitle(title: String) {
        mTitleView.text = title
    }

    fun backClick() =
        mBackView?.clicks()
}